//////////////////////////////////////////////////////////////////////////////////
//	File	:	BaseParticleEffect.cpp
//	Author	:	Yuri Oyoko
//	Purpose	:	Base particle effect is the parent effect for all in-game particle
//				effects (i.e. fire, smoke e.t.c.). BaseParticleEffect defines the
//				way in which the particles in the system behave.
//////////////////////////////////////////////////////////////////////////////////

#include "BaseParticleSystem.h"
#include "Particle.h"
#include "Math.h"
#include "Definitions.h"
#include "SGD Wrappers/CSGD_TextureManager.h"
#include "MainMenuState.h"

BaseParticleSystem::BaseParticleSystem()
{
}
BaseParticleSystem::~BaseParticleSystem()
{
}

void BaseParticleSystem::Initialize()
{
	InitializeConstants();
	ParticlesInSystem = NumberOfEffects * MaxParticleCount;
	Particles = new Particle[ParticlesInSystem];
	for(int i = 0; i < ParticlesInSystem; i++)
	{
		//Particles[i] = new Particle();
		FreeParticles.push_back(&Particles[i]);
	}
}
void BaseParticleSystem::AddParticles(D3DXVECTOR2 Location)
{
	int NumParticles = (int)GetRandomFloat(MinParticleCount, MaxParticleCount);
	for(int i = 0; i < NumParticles && FreeParticles.size() > 0; i++)
	{
		Particle* theParticle = FreeParticles.back();
		FreeParticles.pop_back();
		
		InitializeParticle(theParticle, Location);
	}
}
void BaseParticleSystem::InitializeParticle(Particle* theParticle, D3DXVECTOR2 theLocation)
{
	D3DXVECTOR2 direction = GetRandomDirection();
	float velocity = GetRandomFloat(MinParticleSpeed, MaxParticleSpeed);
	float acceleration = GetRandomFloat(MinParticleAcceleration, MaxParticleAcceleration);
	float lifetime = GetRandomFloat(MinParticleLifetime, MaxParticleLifetime);
	float scale = 0.8f;
	float rotationSpeed = D3DXToRadian(GetRandomFloat(MinParticleRotationSpeed, MaxParticleRotationSpeed));
	float rotation = D3DXToRadian(GetRandomFloat(0, 360));
	theParticle->SetIsActive(true);
	theParticle->SetTimeSinceStart(0);
	theParticle->SecondsTicker = 0;
	theParticle->Initialize(theLocation, velocity * direction, acceleration * direction, lifetime, scale, rotation, rotationSpeed);
	
}
void BaseParticleSystem::Update(float ElapsedTime)
{
	MainMenuState::GetInstance()->SetFreeParticles(FreeParticles.size());
	for(int i = 0; i < ParticlesInSystem; i++)
	{
		if(Particles[i].GetIsActive() == true)
		{
			Particles[i].Update(ElapsedTime);
			if(Particles[i].GetIsActive() == false)
			{
				FreeParticles.push_back(&Particles[i]);
			}
		}
	}
}
void BaseParticleSystem::Render()
{
	for(int i = 0; i < ParticlesInSystem; i++)
	{
		if(Particles[i].GetIsActive() == true)
		{
			//float normalizedLifetime = Particles[i].GetTimeSinceStart() / Particles[i].GetLifetime();
			//float ParticleAlpha;// = 255 * normalizedLifetime * (1 - normalizedLifetime);
			//float v1 = Particles[i].GetTimeSinceStart();
			//ParticleAlpha = (Particles[i].GetTimeSinceStart()/ Particles[i].GetLifetime()) * 255;
			CSGD_TextureManager::GetInstance()->Draw(ParticleTexture, Particles[i].GetPosition().x, Particles[i].GetPosition().y, 0.5f, 0.5f, 0,0,0,0, D3DCOLOR_ARGB(255,255,255,255));
		}
	
	}
}
RECT BaseParticleSystem::GetCollisionRect()
{
	RECT collisionRect = {0,0,0,0};
	return collisionRect;
}
bool BaseParticleSystem::CheckCollision()
{
	return false;
}
D3DXVECTOR2 BaseParticleSystem::GetRandomDirection()
{
	float  theAngle = D3DXToRadian(GetRandomFloat(MinParticleAngle, MaxParticleAngle));
	
	D3DXVECTOR2 theDirection;

	theDirection.x = cos(theAngle);
	theDirection.y = -sin(theAngle);

	return theDirection;
}
